﻿Public Class ucDestino

    Public Property getDestinoSelecionado() As Integer
        Get
            Return cmbDestino.SelectedValue
        End Get
        Set(ByVal value As Integer)
            cmbDestino.SelectedValue = value
        End Set
    End Property
    Public Property getNomeDestinoSelecionado() As String
        Get
            Return CType(cmbDestino.SelectedItem, destinoVO).DESC_DESTINO

        End Get

        Set(ByVal value As String)

            CType(cmbDestino.SelectedItem, destinoVO).DESC_DESTINO = value

        End Set

    End Property


    Public Property getNomeMunicipioSelecionado() As String

        Get
            Return CType(cmbMunicipio.SelectedItem, municipioVO).DESC_MUNICIPIO

        End Get

        Set(ByVal value As String)

            CType(cmbMunicipio.SelectedItem, municipioVO).DESC_MUNICIPIO = value

        End Set

    End Property


    Public Property getIdMunicipioSelecionado() As Integer

        Get
            Return cmbMunicipio.SelectedValue

        End Get

        Set(ByVal value As Integer)

            cmbMunicipio.SelectedValue = value

        End Set

    End Property


    ''Public Property getMunicipioDestino() As String

    ''    Get
    ''        Return CType(cmbDestino.SelectedItem, destinoVO).MUNICIPIO

    ''    End Get

    ''    Set(ByVal value As String)

    ''    End Set

    'End Property

    Private Sub ucDestino_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try

            If Not DesignMode Then

                Dim destinoBO As New destinoBO

                Dim lista As Generic.List(Of destinoVO)

                lista = destinoBO.getDestinos()

                If lista.Count = 0 Then

                    cmbDestino.Items.Insert(0, "Não há destinos cadastrados para esta seção. Favor efetuar o cadastro no registro eletrônico")
                    cmbDestino.SelectedIndex = 0

                Else

                    cmbDestino.DisplayMember = "DESC_DESTINO"
                    cmbDestino.ValueMember = "ID_DESTINO"
                    cmbDestino.DataSource = lista

                End If
            End If

        Catch ex As Exception
            generica.gravaErro(ex)
        End Try

    End Sub

    Private Sub cmbDestino_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cmbDestino.KeyPress
        e.Handled = True
    End Sub




    Private Sub limpaCombo()

        Try

            cmbDestino.Enabled = False
            cmbDestino.DataSource = Nothing
            '  cmbDestino.Items.Clear()
            cmbDestino.SelectedIndex = -1
        Catch ex As Exception

            generica.gravaErro(ex)

        End Try
    End Sub
    Public Sub populaMunicipios(ByRef id_destino As Integer)

        If Not DesignMode Then

            Try



                Dim municipio As New municipioBO
                Dim lista As Generic.List(Of municipioVO)
                lista = municipio.getMunicipios(id_destino)

                If lista.Count = 0 Then

                    cmbMunicipio.DataSource = Nothing
                    cmbMunicipio.Items.Insert(0, "Não há municípios cadastrados para o destino selecionado")
                    cmbMunicipio.SelectedIndex = 0
                    cmbMunicipio.Enabled = False

                Else

                    cmbMunicipio.Enabled = True
                    cmbMunicipio.DisplayMember = "DESC_MUNICIPIO"
                    cmbMunicipio.ValueMember = "ID_MUNICIPIO"
                    cmbMunicipio.DataSource = lista


                End If



            Catch ex As Exception

                generica.gravaErro(ex)

            End Try


        End If

    End Sub

    Private Sub cmbDestino_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbDestino.SelectedValueChanged

        If cmbDestino.SelectedValue > 0 Then
            populaMunicipios(cmbDestino.SelectedValue)
            ' Else

            ' limpaCombo()
        End If

    End Sub

    Public Event cmbDestinoSelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs)

    Private Sub cmbDestino_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmbDestino.SelectionChangeCommitted
        RaiseEvent cmbDestinoSelectionChangeCommitted(sender, e)
    End Sub
End Class
